<?php
class IndexController extends Zend_Controller_Action
{
	
    public function init()
    {
    	Zend_Loader::loadClass('LoginDefaultModel');
    	Zend_Loader::loadClass('SubjectsModel');
    	Zend_Loader::loadClass('TeacherModel');
        Zend_Loader::loadClass('InfoClassModel');
        Zend_Loader::loadClass('sExerciseModel');
    	Zend_Loader::loadClass('Zend_Session');
    	Zend_Loader::loadClass('Zend_Session_Namespace');
        Zend_Loader::loadClass('NoticeModel');
        Zend_Loader::loadClass('PointModel');
    }
    
    public function preDispatch(){
    	
    	/*
    	 * Load template
    	*/
    	$layoutPath = APPLICATION_PATH  . '/templates/default/default';
    	$option = array (	'layout' 		=> 'index',
    			'layoutPath' 	=> $layoutPath );
    	Zend_Layout::startMvc ( $option );
    	
    	$this->view->module = $this->_request->getModuleName();
    	$this->view->controller =  $this->_request->getControllerName();
    	$this->view->action =  $this->_request->getActionName();
    }	
	
    public function indexAction(){
        $lgNamespace = new Zend_Session_Namespace('enrol_teacher');
        $auth = Zend_Auth::getInstance();
        $user = $auth->getIdentity();
        $id_sv= $user->id;
        $error_login = $this->_request->getParam('error_login');
        $this->view->error_login = $error_login;
        
        

        if(($lgNamespace->enrol_teacher !=1)&& ($auth->hasIdentity())&&($_GET['show']=='notice')){
            
        
            $data= NoticeModel::getDbByStudentId($id_sv);
            $paginator = Zend_Paginator::factory($data);
	        $paginator->setItemCountPerPage(10);       
	        $paginator->setPageRange(3);
	        $currentPage = $this->_request->getParam('page',1);
	        $paginator->setCurrentPageNumber($currentPage);
	        $this->view->data=$paginator;
            $this->view->show= 1;
          
        }else if(($lgNamespace->enrol_teacher !=1)&& ($auth->hasIdentity())){
             
            $model= new InfoClassModel();
            $exerciseDb= $model->getAllExforStu($id_sv);
            $paginator = Zend_Paginator::factory($exerciseDb);
	        $paginator->setItemCountPerPage(5);       
	        $paginator->setPageRange(3);
	        $currentPage = $this->_request->getParam('page',1);
	        $paginator->setCurrentPageNumber($currentPage);
	        $this->view->data=$paginator;
            $this->view->id_sv= $id_sv;
          }
        else if(($lgNamespace->enrol_teacher ==1) && ($auth->hasIdentity())){
           
            $page = $this->_request->getParam('page');
            if(!$page) $offset = 1;
            else $offset = $page;
            $this->view->now_page = $offset;
            $this->view->count = count(NoticeModel::getAllnotice());
            $notice = NoticeModel::selectLimitStudent(30,($offset-1)*30);
            $this->view->notice = $notice;
        }
    }
    
    public function getAllCourseAction(){
    	$auth = Zend_Auth::getInstance();
    	$lgNamespace = new Zend_Session_Namespace('enrol_teacher');
    	if($lgNamespace->enrol_teacher ==1){
	    	if ($auth->hasIdentity()) {
	    		$user = $auth->getIdentity();
	    		$list = SubjectsModel::getSubjectOfTeacher($user->id);
	    		$this->view->list = $list;
	    	}
    	}
    	else{
    		echo "Not enrol";die;
    	}
    	
    }
    
    /*
     * Xử lý đăng nhập
    */
    
    public function loginAction(){
    	/*
    	 * Set layout moi cho GUI
    	*/
    //	 echo 12;die;
    	$this->view->headTitle()->set('Đăng nhập');
    	$layoutPath = APPLICATION_PATH  . '/templates/admin/admin';
    	$option = array (	'layout' 		=> 'login',
    			'layoutPath' 	=> $layoutPath );
    	Zend_Layout::startMvc ( $option );
    
    	$request = $this->getRequest();
    	$view = $this->view;
    	/*
    	 * Khi có request từ client tiến hành nhận dữ liệu gửi lên và xử lý
    	*/
        try{
        	if ($request->isPost()) {
        		/*
        		 * Nhận dữ liệu đầu vào.
        		*/
        		$user_name = $request->getParam('username');
        		$password = $request->getParam('password');
        		$this->view->user = $user_name;
        		$this->view->pass = $password;
        		$loginModel = new LoginDefaultModel();
        		/*
        		 * Kiểm tra đăng nhập
        		*/
        		$result_check = $loginModel->login($user_name,$password);
        		if (!$result_check) {
        		   $this->_redirect('/index/index?error_login=1');
        		}
        		else{
        			if ($this->_request->getParam('remember') == true){
        				require_once 'Zend/Session.php';
        				$time = 60 * 60 * 24 * 7 * 30;
        				Zend_Session::rememberMe($time);
        			}else {
        				Zend_Session::forgetMe();
        			}
        			$lgNamespace = new Zend_Session_Namespace('enrol_teacher');
        			if(strcmp($result_check, "Teacher") == 0)
        				$lgNamespace->enrol_teacher = 1;
        			else $lgNamespace->enrol_teacher = 0;
        		}
        		$this->_redirect('/index/index');
        	}
        }
        catch(exception $e)
        {
           echo "Có lỗi xảy ra";die;
        }
   // 	$this->_redirect('/index/index');
    }
    
    /*
     * Xử lý logout khỏi tài khoản
    */
    
    public function logoutAction(){
    	$auth = Zend_Auth::getInstance();
    	$auth->clearIdentity();
    	$this->_redirect("/index/index");
        $lgNamespace = new Zend_Session_Namespace('enrol_teacher');
        $lgNamespace->enrol_teacher = -1;
    }

     /*
     * Xử lý xóa notice.
     */
    
    function deleteNoticeAction(){
        $dt = new NoticeModel();
        $request = $this->getRequest();
        $id = $request->getParam('id');
        if($id){
            $dt->delete("id=".$id);
        }
        exit();
    }
 
}










